package com.victor.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.Footprint;
import com.victor.entity.Jingqucomment;
import com.victor.entity.Jingqulist;
import com.victor.entity.JqCollection;
import com.victor.entity.JqReply;
import com.victor.entity.JqcommentPraise;
import com.victor.entity.JqcommentsReply;
import com.victor.service.FootprintService;
import com.victor.service.JingquService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;
/**
 * 景区
 * @author 
 *
 */
@RestController
@RequestMapping("/jingqu")
public class JingquAppController {
	@Autowired
	private JingquService jingquService;
	@Autowired
	private FootprintService footprintService;
	
	/**
	 * 景区详情
	 * @param jqid
	 * @param uid
	 * @param page
	 * @param row
	 * @param flag
	 * @return
	 */
	@RequestMapping("/appdetail")
	public Msg xiangqing(String jqid,String uid,int page,int row,String flag) {
		if (jqid == null || jqid.isEmpty() || flag == null || flag.isEmpty()) {
			return Msg.fail("入参失败");
		}
		if (uid == null || uid.isEmpty()) {
			Jingqulist jingqulist = jingquService.selByJqid(jqid);
			PageInfo<Jingqucomment> selJingquComments = jingquService.selJingquComments(jqid,page,row,flag);
			return Msg.success().add("jingqu", jingqulist).add("comments", selJingquComments);
		}else {
			Jingqulist selByJqidAndUid = jingquService.selByJqidAndUid(jqid,uid);
			PageInfo<Jingqucomment> seljingquCommentsByUid = jingquService.seljingquCommentsByUid(jqid,page,row,flag,uid);
			return Msg.success().add("jingqu", selByJqidAndUid).add("comments", seljingquCommentsByUid);
		}
	}
	/**
	 * 收藏景区
	 * @param collection
	 * @return
	 */
	@RequestMapping("/jqcollection")
	public Msg collectionJq(JqCollection collection) {
		String[] strings = {"uid","jqid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(collection, strings);
		if (empty == true) {
			return Msg.fail("入参失败");
		}
		JqCollection selCollection = jingquService.selCollection(collection);
		if (selCollection == null) {
			boolean b = jingquService.insertJqCollection(collection);
			if (!b) {
				return Msg.fail("收藏失败");
			}
			return Msg.success().add("return", "收藏成功");
		}else {
			boolean delJqCollection = jingquService.delJqCollection(selCollection.getId());
			if (!delJqCollection) {
				return Msg.fail("取消收藏失败");
			}
			return Msg.success().add("return", "取消收藏成功");
		}
		
	}
	/**
	 * 景区评论点赞//取消点赞
	 * @param praise
	 * @return
	 */
	@RequestMapping("/praise")
	public Msg jqCommentsPraise(JqcommentPraise praise) {
		String[] str = {"uid","jcid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(praise, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		JqcommentPraise commentsPraise = jingquService.selJqCommentsPraise(praise);
		if (commentsPraise == null) {
			boolean insertJqCommentsPraise = jingquService.insertJqCommentsPraise(praise);
			if (!insertJqCommentsPraise) {
				return Msg.fail("点赞失败");
			}
			String selPraiseCount = jingquService.selPraiseCount(praise.getJcid());
			return Msg.success().add("praisecount", selPraiseCount);
		}else {
			boolean delCommentsPraise = jingquService.delCommentsPraise(commentsPraise.getId());
			if (!delCommentsPraise) {
				return Msg.fail("取消点赞失败");
			}
			String selPraiseCount = jingquService.selPraiseCount(praise.getJcid());
			return Msg.success().add("praisecount", selPraiseCount);
		}
		
	}
	/**
	 * 评论景区
	 * @param cJingqucomment
	 * @return
	 */
	@RequestMapping("/jingqu")
	public Msg jqComment(@RequestBody Jingqucomment cJingqucomment) {
		String[] str = {"jqid","uid","flag"};
		boolean empty = ClassFilter.somePropertyIsEmpty(cJingqucomment, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean insertJqComments = jingquService.insertJqComments(cJingqucomment);
		if (!insertJqComments) {
			return Msg.fail("添加评论失败");
		}
		if (cJingqucomment.getJingqucommentphotos() != null || cJingqucomment.getJingqucommentphotos().size()>0) {
			for (int i = 0; i < cJingqucomment.getJingqucommentphotos().size(); i++) {
				cJingqucomment.getJingqucommentphotos().get(i).setJqcid(String.valueOf(cJingqucomment.getId()));
				boolean insertJqCommentsPhoto = jingquService.insertJqCommentsPhoto(cJingqucomment.getJingqucommentphotos().get(i));
				if (!insertJqCommentsPhoto) {
					return Msg.fail("添加评论图片失败");
				}
			}
		}
		return Msg.success();
		
	}
	/**
	 * 回复评论
	 * @param reply
	 * @return
	 */
	@RequestMapping("/replycomments")
	public Msg jqCommentsReply(JqcommentsReply reply) {
		String[] str = {"uid","cid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(reply, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean b = jingquService.insertJqcommentsReply(reply);
		return Msg.judge(b);
	}
	
	/**
	 * 回复 评论的回复
	 * @param reply
	 * @return
	 */
	@RequestMapping("/reply")
	public Msg jqReply(JqReply reply) {
		String[] str = {"uid","rid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(reply, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		return Msg.judge(jingquService.insertReply(reply));
	}

	/**
	 * 景区首页
	 * @param city
	 * @param search
	 * @param jqclassid
	 * @param tjflag
	 * @param jqlevel
	 * @param price
	 * @param page
	 * @param row
	 * @param uid
	 * @return
	 */
	@RequestMapping("/jqfirstpage")
	public Msg firstPageJingqu(String city,String search,String jqclassid,String tjflag,String jqlevel,String price,int page,int row,String uid) {
		if (city == null || city.isEmpty()) {
			return Msg.fail("入参错误");
		}
		List<Jingqulist> selByMuchCondition = jingquService.selByMuchCondition(city,search,jqclassid,tjflag,jqlevel,price);
		if (uid == null || uid.isEmpty()) {
			PageInfo<Jingqulist> selJqYouLike = jingquService.selJqYouLike(city,page,row);
			return Msg.success().add("jingqu", selByMuchCondition).add("like", selJqYouLike);
		}else {
			List<Footprint> list = footprintService.selByCityAndUid(city,uid);
			List<Jingqulist> jqlist = new ArrayList<>();
			if (list == null || list.size()<=0) {
				PageInfo<Jingqulist> selJqYouLike = jingquService.selJqYouLike(city,page,row);
				return Msg.success().add("jingqu", selByMuchCondition).add("like", selJqYouLike);
			}else {
				for (int i = 0; i < list.size(); i++) {
					Jingqulist selByJqid = jingquService.selByJqid(list.get(i).getPid());
					jqlist.add(selByJqid);
				}
				return Msg.success().add("jingqu", selByMuchCondition).add("like", jqlist);
			}
			
		}
		
	}
}
